<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>GrPPI: Farm pattern</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">1.0</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Farm pattern </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <b>farm</b> pattern (or <em>task-farm</em>) is a streaming pattern that applies an operation to every element in a stream. This streaming pattern can only be used inside another pattern and consequently does not take an execution policy itself, but uses the execution policy of its enclosing pattern.</p>
<p>The interface to the <b>farm</b> pattern is provided by function <code><a class="el" href="group__farm__pattern.html#ga2e0164a8852fb54244cda390dc68d10f" title="Invoke Farm pattern on a data stream that can be composed in other streaming patterns.">grppi::farm()</a></code>.</p>
<div class="fragment"><div class="line"><a class="code" href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a>(exec,</div>
<div class="line">  stage1,</div>
<div class="line">  <a class="code" href="group__farm__pattern.html#ga2e0164a8852fb54244cda390dc68d10f">grppi::farm</a>(arguments...),</div>
<div class="line">  stage3,</div>
<div class="line">  stage4);</div>
<div class="ttc" id="agroup__farm__pattern_html_ga2e0164a8852fb54244cda390dc68d10f"><div class="ttname"><a href="group__farm__pattern.html#ga2e0164a8852fb54244cda390dc68d10f">grppi::farm</a></div><div class="ttdeci">auto farm(int ntasks, Transformer &amp;&amp;transform_op)</div><div class="ttdoc">Invoke Farm pattern on a data stream that can be composed in other streaming patterns.</div><div class="ttdef"><b>Definition:</b> farm.h:40</div></div>
<div class="ttc" id="agroup__pipeline__pattern_html_gaf3942c499118a951617b95c4ee10a502"><div class="ttname"><a href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a></div><div class="ttdeci">void pipeline(const Execution &amp;ex, Generator &amp;&amp;generate_op, Transformers &amp;&amp;... transform_ops)</div><div class="ttdoc">Invoke Pipeline pattern on a data stream.</div><div class="ttdef"><b>Definition:</b> pipeline.h:46</div></div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md615"></a>
Farm variants</h1>
<p>There is a single variant:</p>
<ul>
<li>Composable <em>farm</em>: Defines a farm that can be used as a building block by another pattern (e.g. a <em>pipeline</em>).</li>
</ul>
<h1><a class="anchor" id="autotoc_md616"></a>
Key elements in a farm</h1>
<p>The key elements of a <b>farm</b> are the <b>cardinality</b> and the <b>Transformer</b>.</p>
<p>The <b>cardinality</b> is the number of replicas of the farm that can be concurrently executed.</p>
<p>The central element in a farm is the <b>Transformer</b>. The operation may be any C++ callable entity. This operation, is a unary operation taking a data item and returning its transformation. Thus, a transformer <code>op</code> is any operation that, given an input value <code>x</code> of type <code>T</code> makes valid the following:</p>
<div class="fragment"><div class="line">U res{transformer(x)};</div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md617"></a>
Details on farm variants</h1>
<h2><a class="anchor" id="autotoc_md618"></a>
Composable farm</h2>
<p>A <em>composable farm</em> applies a <b>Transformer</b> to each data item in a stream. The <b>farm</b> can be inserted into another upper level pattern (which will be responsible for generation and consumption)</p>
<hr  />
<p> <b>Example</b>: Use a farm as a stage of a composed pipeline. </p><div class="fragment"><div class="line"><a class="code" href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a>(exec,</div>
<div class="line">  stageA,</div>
<div class="line">  stageB,</div>
<div class="line">  <a class="code" href="group__farm__pattern.html#ga2e0164a8852fb54244cda390dc68d10f">grppi::farm</a>(4, [](<span class="keyword">auto</span> x) {</div>
<div class="line">    <span class="keywordflow">return</span> x.length();</div>
<div class="line">  }),</div>
<div class="line">  stageC</div>
<div class="line">);</div>
</div><!-- fragment --> <hr  />
<p> <b>Note</b>: For brevity we do not show here the details of other stages.</p>
<p>For composing complex patterns, the <code><a class="el" href="group__farm__pattern.html#ga2e0164a8852fb54244cda390dc68d10f" title="Invoke Farm pattern on a data stream that can be composed in other streaming patterns.">farm()</a></code> function may be used to create an object that may be used later in the composition.</p>
<hr  />
<p> <b>Example</b>: Build a farm as a composable pattern and use later as a stage of a pipeline. </p><div class="fragment"><div class="line"><span class="keyword">auto</span> print_long_words = <a class="code" href="group__farm__pattern.html#ga2e0164a8852fb54244cda390dc68d10f">grppi::farm</a>(3, [](<span class="keyword">auto</span> x) {</div>
<div class="line">  <span class="keywordflow">if</span> (x.length() &gt; 4) std::cout &lt;&lt; x &lt;&lt; std::endl;</div>
<div class="line">});</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__pipeline__pattern.html#gaf3942c499118a951617b95c4ee10a502">grppi::pipeline</a>(exec,</div>
<div class="line">  stageA,</div>
<div class="line">  stageB,</div>
<div class="line">  print_long_words,</div>
<div class="line">  stageC</div>
<div class="line">);</div>
</div><!-- fragment --> <hr  />
<p> <b>Note</b>: For brevity we do not show here the details of other stages. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2
</small></address>
</body>
</html>
